package top.mccat.common.core.controller.bk;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.mccat.common.annotation.Log;
import top.mccat.common.core.controller.BaseController;
import top.mccat.common.core.domain.AjaxResult;
import top.mccat.common.core.domain.bk.BkSale;
import top.mccat.common.enums.BusinessType;
import top.mccat.common.exception.bk.BaseItemNotEnoughException;
import top.mccat.common.exception.bk.BaseItemNotFoundException;
import top.mccat.common.service.IBkSaleService;
import top.mccat.common.utils.poi.ExcelUtil;
import top.mccat.common.core.page.TableDataInfo;

/**
 * 销售记录Controller
 * 
 * @author Raven
 * @date 2023-01-10
 */
@RestController
@RequestMapping("/bk/sale")
public class BkSaleController extends BaseController
{
    @Autowired
    private IBkSaleService bkSaleService;

    /**
     * 查询销售记录列表
     */
    @PreAuthorize("@ss.hasPermi('bk:sale:list')")
    @GetMapping("/list")
    public TableDataInfo list(BkSale bkSale)
    {
        startPage();
        List<BkSale> list = bkSaleService.selectBkSaleList(bkSale);
        return getDataTable(list);
    }

    /**
     * 导出销售记录列表
     */
    @PreAuthorize("@ss.hasPermi('bk:sale:export')")
    @Log(title = "销售记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BkSale bkSale)
    {
        List<BkSale> list = bkSaleService.selectBkSaleList(bkSale);
        ExcelUtil<BkSale> util = new ExcelUtil<BkSale>(BkSale.class);
        util.exportExcel(response, list, "销售记录数据");
    }

    /**
     * 获取销售记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('bk:sale:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(bkSaleService.selectBkSaleById(id));
    }

    /**
     * 新增销售记录
     */
    @PreAuthorize("@ss.hasPermi('bk:sale:add')")
    @Log(title = "销售记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BkSale bkSale)
    {
        AjaxResult ajaxResult;
        try {
            ajaxResult = toAjax(bkSaleService.insertBkSale(bkSale));
        } catch (BaseItemNotEnoughException | BaseItemNotFoundException e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return ajaxResult;
    }

    /**
     * 修改销售记录
     */
    @PreAuthorize("@ss.hasPermi('bk:sale:edit')")
    @Log(title = "销售记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BkSale bkSale)
    {
        return toAjax(bkSaleService.updateBkSale(bkSale));
    }

    /**
     * 删除销售记录
     */
    @PreAuthorize("@ss.hasPermi('bk:sale:remove')")
    @Log(title = "销售记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(bkSaleService.deleteBkSaleByIds(ids));
    }
}
